
*------------------------------------------------------------------------------*
* Model minority governments
*
* Version:	2016-06-10
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


* Parse anti-system parties
local antisys "$antisys"
*di "`antisys'"
tokenize "`antisys'", parse(",")
local antisys1 `1'
local antisys2 `3'
local antisys3 `5'
local antisys4 `7'
local antisys5 `9'
*di "`antisys1'"
*di "`antisys2'"
*di "`antisys3'"
*di "`antisys4'"
*di "`antisys5'"


* Mark anti-system parties
if "`antisys1'" == "no" {
	gen antisys = .
	}
else {
	gen antisys = .
	replace antisys = 1 if p101 == `antisys1'
	capture: replace antisys = 1 if p101 == `antisys2'
	capture: replace antisys = 1 if p101 == `antisys3'
	capture: replace antisys = 1 if p101 == `antisys4' 
	capture: replace antisys = 1 if p101 == `antisys5' 
}


* Estimate absolute majority without NONAs and parties with missing ideological data
gen filterseat = p303 if $ideology !=.
by g101 g105, sort: egen zs1 = sum(filterseat)
gen abs_maj = zs1 / 2


* Seats and position of government (coalition)
gen filterseat2 = p303 if p103 == 1 & $ideology != .
by g101 g105, sort: egen newgovseat = sum(filterseat2)


* Distance of all parties to government
gen party_range = abs($ideology - GOV_POS)
	* Reset distance...
replace party_range = 0 if p103==1														// of actual government parties to Zero
replace party_range = 999 if $ideology == . | p118<2 | filterseat<1 | filterseat == .	// of parties with missing ideology or not in legislature
replace party_range = 999 if antisys == 1 & p103 != 1									// of anti-system parties (if specified), unless they actually participated in government
	* ... and sort ascending
sort g101 g105 party_range p303


* Sum up seats
gen sumupseats = newgovseat if p103 == 1 & $ideology != .
replace sumupseats = sumupseats[_n-1] + p303 if g101[_n-1] == g101 & g105[_n-1] == g105 & p103 == .
	* Mark participants necessary for obtaining absolute majority
gen minogov_dummy = 1 if p103 == 1 & $ideology != .
replace minogov_dummy = 1 if sumupseats < abs_maj
replace minogov_dummy = 1 if minogov_dummy[_n-1] == 1 & sumupseats[_n-1] < abs_maj


* Estimate weighted positions
by g101 g105, sort: egen minogovtotal = sum(filterseat) if minogov_dummy == 1
gen minoweight = filterseat / minogovtotal
	* Weigh with position
gen zs2 = minoweight * GOV_POS if p103 == 1								// weighted GOV_POS of actual government
replace zs2 = minoweight * $ideology if minogov_dummy == 1 & p103 != 1	// "add" minority partner(s) with its/their share
by g101 g105, sort: egen gov_pos_mino = sum(zs2)


* Check, whether government has an absolute majority on its own
gen minoritygov = .
replace minoritygov = 0 if newgovseat >= abs_maj				// government has own majority
replace minoritygov = 1 if newgovseat <  abs_maj				// government depends on support
replace minoritygov = 0 if newgovseat == 0 & gov_pos_mino == 0  // reset expert governments
replace minoritygov = . if g106 == 9


* Replace GOV_POS with position of temporary minimal connected winning coalition if it is a minority government
replace GOV_POS = gov_pos_min if minoritygov == 1


* Cleansing
replace GOV_POS 	  = . if g106 == 9
replace minogov_dummy = . if g106 == 9
replace minoritygov	  = . if g106 == 9

lab var minogov_dummy "Dummy: 1 = Party supporting minority government"
lab var minoritygov   "Dummy: 1 = Minority government relied on support"

sort g101 g105 p101

capture: macro drop antisys
capture: drop antisys-sumupseats  minogovtotal-gov_pos_mino
